import os
import re
path='E:/use/test/note/数学md'
name=path+'/重积分 copy.md'
print(os.getcwd())
# 默认打开的是note文件夹
with open(name,'r',encoding='utf-8')as file:
    lines=file.read()
print(lines)
for i in range(100):
    lines=re.sub('\$ ','$',lines)
    lines=re.sub(' \$','$',lines)
lines=re.sub('#\$','# $',lines)
lines=re.sub('\$\$','\n$$\n',lines)
with open(name,'w',encoding='utf-8')as file:
    file.write(lines)
# pattern = re.compile(r'\$')
# use=pattern.finditer(lines)
# now1=0
# left=0
# right=0
# for i in use:
#     print(i.span())
#     now1=now1+1
#     if(now1%2==0):
#      right=i.span()[0]
#      t=lines[left:right] 
#      print(t)
#      print(left)
#      print(right)
#      first=1
#      ss=''
#      for k in t.split():
#          if(first):
#            ss=ss+k
#            first=0
#          else:
#             ss=ss+' '+k
#      lines=re.sub(t,ss,lines)
#     # print(lines)
#     else:
#      left=i.span()[1]
#①替换 $和 $，直到没有
#②把#$替换成# $
#在$$前后加上换行
